const Koa = require("koa");
const { koaBody } = require("koa-body");
const path = require("path");
const router = require("../router");
const KoaStatic = require("koa-static");
const err = require("./err");
const app = new Koa();
const parameter = require('koa-parameter');

// 在路由之前使用 koaBody
app.use(
  koaBody({
    multipart: true,
    formidable: {
      uploadDir: path.join(__dirname, '../upload'),
      keepExtensions: true,
      maxFileSize: 2 * 1024 * 1024,
    },
    parsedMethods: ['POST', 'PUT', 'PATCH', 'DELETE']
  })
);

// 指定静态文件目录
app.use(KoaStatic(path.join(__dirname, '../upload'))); // 添加静态文件目录

// 使用 parameter 中间件
app.use(parameter(app));

app.use(router.routes()).use(router.allowedMethods());
app.on("error", err);

module.exports = app;